<?php
/**
 * Created by PhpStorm.
 * User: manageistrator
 * Date: 2017/2/27
 * Time: 17:21
 */
namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Manage\BaseController;
use App\Repositories\Manage\MenuRepo;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\URL;

class MenuController extends BaseController
{
    public $menuId = 1;

    public function menuList(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'menuList');
        $data = $menuRepo->getMenuList();
        return view('manage.menu.menu_list', ['data' => $data]);
    }

    public function menuAdd(MenuRepo $menuRepo)
    {
        $this->actionAuthorize($this->menuId, 'menuAdd');
        $firstMenuData = $menuRepo->getFirstMenuList();
        return view('manage.menu.menu_add', ['firstMenuData' => $firstMenuData]);
    }

    public function menuPostAdd(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'menuAdd');
        $name = $request->get('name');
        $url = $request->get('url');
        $sort = $request->get('sort');
        $sort = empty($sort) ? '50' : $sort;
        $firstMenuId = $request->get('first_menu');
        try {
            $menuRepo->addMenu($name, $url, $sort , $firstMenuId);
            return $this->ajaxResponse(0, '添加成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '添加失败' . $e->getMessage());
        }

    }

    public function menuEdit(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'menuEdit');
        $id = $request->get('id');
        $data = $menuRepo->getMenu('id = ?', [$id]);
        $firstMenuData = $menuRepo->getFirstMenuList();
        return view('manage.menu.menu_edit', ['data' => $data, 'firstMenuData' => $firstMenuData]);
    }

    public function menuPostEdit(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'menuEdit');
        $id = $request->get('id');
        $name = $request->get('name');
        $url = $request->get('url');
        $sort = $request->get('sort');
        $sort = empty($sort) ? '50' : $sort;
        $firstMenuId = $request->get('first_menu');
        try {
            $menuRepo->updateMenu($id, $name, $url, $sort , $firstMenuId);
            return $this->ajaxResponse(0, '修改成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '修改失败' . $e->getMessage());
        }

    }


    public function menuModuleList(MenuRepo $menuRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'menuModuleList');
        $id = $request->get('id');
        $menuData = $menuRepo->getMenu('id = ?', [$id]);
        $data = $menuRepo->getModuleList('menu_id = ?', [$id]);
        return view('manage.menu.module_list', ['data' => $data, 'menu_id' => $id, 'name' => $menuData->name]);
    }


    public function modulePostAdd(MenuRepo $menuRepo, Request $request)
    {

        $this->actionAuthorize($this->menuId, 'modulePostAdd');
        $menuId = $request->get('menuId');
        $moduleName = $request->get('moduleName');
        $value = $request->get('value');
        $action = $request->get('action');
        if (!is_numeric($value) || $value <= 0) {
            return $this->ajaxResponse(1, '权限值必须为大于0的数字');
        }
        $row = $value & ($value - 1);
        if ($row != 0) {
            return $this->ajaxResponse(1, '权限值不正确，请填写2的次幂');
        }

        $data = $menuRepo->getModule('menu_id = ? and value = ?', [$menuId, $value]);
        if (!empty($data)) {
            return $this->ajaxResponse(1, '该菜单下已存在相同的权限值，请重新填写');
        }

        try {
            $menuRepo->addMenuModule($menuId, $moduleName, $value, $action);
            return $this->ajaxResponse(0, '添加成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '添加失败' . $e->getMessage());
        }

    }

    public function moduleEdit(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'moduleEdit');
        $id = $request->get('id');
        $data = $menuRepo->getModule('id = ?', [$id]);
        $menuData = $menuRepo->getMenu('id = ?', [$data->menu_id]);
        $menuName = $menuData->name;
        return view('manage.menu.module_edit', ['data' => $data, 'id' => $id, 'menuName' => $menuName]);

    }


    public function modulePostEdit(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'moduleEdit');
        $id = $request->get('id');
        $menuId = $request->get('menuId');
        $moduleName = $request->get('moduleName');
        $value = $request->get('value');
        $action = $request->get('action');
        if (!is_numeric($value) || $value <= 0) {
            return $this->ajaxResponse(1, '权限值必须为大于0的数字');
        }
        $row = $value & ($value - 1);
        if ($row != 0) {
            return $this->ajaxResponse(1, '权限值不正确，请填写2的次幂');
        }
        $data = $menuRepo->getModule('menu_id = ? and value = ?', [$menuId, $value]);
        if (!empty($data) && $data->id != $id) {
            return $this->ajaxResponse(1, '该菜单下已存在相同的权限值，请重新填写');
        }

        try {
            $menuRepo->editMenuModule($id, $moduleName, $value, $action);
            return $this->ajaxResponse(0, '修改成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '修改失败' . $e->getMessage());
        }

    }


    public function firstMenuList(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'firstMenuList');
        $data = $menuRepo->getFirstMenuList();
        return view('manage.menu.first_menu_list', ['data' => $data]);


    }

    public function firstMenuAdd()
    {
        $this->actionAuthorize($this->menuId, 'firstMenuAdd');
        return view('manage.menu.first_menu_add');
    }

    public function firstMenuPostAdd(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'firstMenuAdd');
        $name = $request->get('name');
        if (empty($name)) {
            return $this->ajaxResponse(1, '一级菜单名称必须填写');
        }
        $class = $request->get('class');
        $sort = $request->get('sort');
        $sort = empty($sort) ? '50' : $sort;
        try {
            $menuRepo->addFirstMenu(['name' => $name, 'class' => $class, 'sort' => $sort]);
            return $this->ajaxResponse(0, '添加成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '添加失败' . $e->getMessage());
        }

    }


    public function firstMenuEdit(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'firstMenuEdit');
        $id = $request->get('id');
        $data = $menuRepo->getFirstMenu($id);
        return view('manage.menu.first_menu_edit', ['data' => $data]);
    }


    public function firstMenuPostEdit(MenuRepo $menuRepo, Request $request)
    {
        $this->actionAuthorize($this->menuId, 'firstMenuEdit');
        $name = $request->get('name');
        $id = (int)$request->get('id');
        if (empty($id)) {
            return $this->ajaxResponse(1, '缺少id参数');
        }
        if (empty($name)) {
            return $this->ajaxResponse(1, '一级菜单名称必须填写');
        }
        $class = $request->get('class');
        $sort = $request->get('sort');
        $sort = empty($sort) ? '50' : $sort;
        try {
            $menuRepo->editFirstMenu($id, ['name' => $name, 'class' => $class, 'sort' => $sort]);
            return $this->ajaxResponse(0, '修改成功');
        } catch (\Exception $e) {
            return $this->ajaxResponse(1, '修改失败' . $e->getMessage());
        }

    }


}